package com.ruoyi.common.mybatis.config.handler;//package com.ruoyi.common.core.config.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.BaseEntity;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;


public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {

    //    @Override
//    public void insertFill(MetaObject metaObject) {
//        //根据属性名字设置要填充的值
//        if (metaObject.hasGetter("createTime")) {
//            if (metaObject.getValue("createTime") == null) {
//                this.setFieldValByName("createTime", new Date(), metaObject);
//            }
//        }
//        if (metaObject.hasGetter("createBy")) {
//            if (metaObject.getValue("createBy") == null) {
//                this.setFieldValByName("createBy", SecurityUtils.getUsername(), metaObject);
//            }
//        }
//    }
//@Override
//public void updateFill(MetaObject metaObject) {
//    if (metaObject.hasGetter("updateBy")) {
//        if (metaObject.getValue("updateBy") == null) {
//            this.setFieldValByName("updateBy", SecurityUtils.getUsername(), metaObject);
//        }
//    }
//    if (metaObject.hasGetter("updateTime")) {
//        if (metaObject.getValue("updateTime") == null) {
//            this.setFieldValByName("updateTime", new Date(), metaObject);
//        }
//    }
//}
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            //根据属性名字设置要填充的值
            if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
                Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) ? baseEntity.getCreateTime() : new Date();
                baseEntity.setCreateTime(current);
                baseEntity.setUpdateTime(current);
                String username = StringUtils.isNotBlank(baseEntity.getCreateBy()) ? baseEntity.getCreateBy() : getLoginUsername();
                //当前已登录 且创建人为空 则填充
                baseEntity.setCreateBy(username);
                //当前已登录 且更新人为空 则填充
                baseEntity.setUpdateBy(username);
            }
        } catch (Exception e) {
            throw new ServiceException("自动注入异常=>" + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
                Date current = new Date();
                //更新时间填充
                baseEntity.setUpdateTime(current);
                String username = getLoginUsername();
                //当前已登录 更新人填充
                if (StringUtils.isNotBlank(username)) {
                    baseEntity.setUpdateBy(username);
                }
            }
        } catch (Exception e) {
            throw new ServiceException("自动注入异常 => "+e.getMessage(),HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    private String getLoginUsername() {
        String username = SecurityUtils.getUsername();
        return StringUtils.isNotBlank(username) ? username : null;
    }
}
